package ${package.Parent}.impl;

import ${package.Parent}.dao.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import ${superServiceImplClassPackage};
import org.springframework.stereotype.Service;
import ${package.Parent}.vo.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;
import java.util.List;

/**
 * ${table.comment!} 服务实现类
 *
 * @author ${author}
 * @since ${date}
 */
@Slf4j
@Service
<#if kotlin>
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} {

}
<#else>
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} {

    @Autowired
    private ${table.mapperName} ${entity?uncap_first}Mapper;

    /***
     * 条件分页查询${entity?uncap_first}数据
     * @param ${entity?uncap_first} 查询条件实体
     * @param page 当前页数
     * @param size 每页大小
     * @return 查询结果
     */
    public Result findPage(${entity} ${entity?uncap_first}, int page, int size) {
        Page<${entity}> pageInfo = page(new Page<${entity}>(page, size),
                new LambdaQueryWrapper<${entity}>(${entity?uncap_first}).orderByDesc(${entity}::getGmtCreate));
        return Result.OK("查询成功", pageInfo);
    }


    /***
     * 分页查询所有${entity?uncap_first}数据
     * @param page 当前页数
     * @param size 每页大小
     * @return 查询结果
     */
    public Result findPage(int page, int size) {
        Page<${entity}> pageInfo = page(new Page<>(page, size));
        return Result.OK("查询成功", pageInfo);
    }


    /***
     * 条件查询${entity?uncap_first}数据
     * @param ${entity?uncap_first} 查询条件实体
     * @return 查询结果
     */
    public Result findList(${entity} ${entity?uncap_first}) {
        List<${entity}> ${entity?uncap_first}List = list(new LambdaQueryWrapper<${entity}>(${entity?uncap_first}).orderByDesc(${entity}::getGmtCreate));
        return Result.OK("查询成功", ${entity?uncap_first}List);
    }


    /***
     * 根据Id删除${entity?uncap_first}数据
     * @param id 删除的数据Id
     * @return 删除结果
     */
    @Transactional
    public Result deleteById(Integer id) {
        ${entity} one = getById(id);
        if (one != null) {
            removeById(id);
            return Result.OK("删除成功");
        } else {
            return Result.ERROR("数据不存在！");
        }
    }


    /***
     * 根据Id批量删除${entity?uncap_first}数据
     * @param ids 删除的数据Id集合
     * @return 删除结果
     */
    @Transactional
    public Result deleteByIds(List<Integer> ids) {
        removeByIds(ids);
        return Result.OK("删除成功");
    }


    /***
     * 根据Id修改${entity}数据
     * @param ${entity?uncap_first} 修改数据实体
     * @return 修改结果
     */
    @Transactional
    public Result update(${entity} ${entity?uncap_first}) {
        ${entity} one = getById(${entity?uncap_first}.getId());
        if (one != null) {
            updateById(${entity?uncap_first});
            return Result.OK("修改成功", ${entity?uncap_first});
        } else {
            return Result.ERROR("数据不存在！");
        }

    }


    /***
     * 新增${entity}数据
     * @param ${entity?uncap_first} 新增数据实体
     * @return 新增结果
     */
    @Transactional
    public Result add(${entity} ${entity?uncap_first}) {
        ${entity} one = getById(${entity?uncap_first}.getId());
        if (one == null) {
            save(${entity?uncap_first});
            return Result.OK("添加成功", ${entity?uncap_first});
        } else {
            return Result.ERROR("数据已存在！");
        }
    }


    /***
     * 根据ID查询${entity}数据
     * @param id 查询Id编号
     * @return 查询结果
     */
    public Result findById(Integer id) {
        ${entity} ${entity?uncap_first} = getById(id);
        return Result.OK("查询成功", ${entity?uncap_first});
    }


    /***
     * 根据Id批量查询${entity}数据
     * @param ids 查询的数据Id集合
     * @return 查询结果
     */
    public Result findByIds(List<Integer> ids) {
        List<${entity}> ${entity?uncap_first}List = listByIds(ids);
        return Result.OK("查询成功", ${entity?uncap_first}List);
    }


    /***
     * 查询所有${entity}
     * @return 查询结果
     */
    public Result findAll() {
        List<${entity}> ${entity?uncap_first}List = list(new LambdaQueryWrapper<${entity}>().orderByDesc(${entity}::getGmtCreate));
        return Result.OK("查询成功", ${entity?uncap_first}List);
    }
    
}
</#if>
